< previous page page_34 next page >

Page 34
Writing Program Code
The procedure that calculates the loan payment will be more complex than the Exit procedure. Obviously, the code that performs the payment calculation will be more involved than a simple End statement. In addition, you must do some additional housekeeping in this procedure, as you will be using variables.
A variable is a temporary storage location for information. Very often, your programs will need to remember informationsuch as calculation results, the user's name, order totals, and so onas the program is running. Think of a variable like a white board that the program can use to remember information. Within certain guidelines, the program can write information on the white board, modify that information, even erase it completely. One important consideration is that when the program finishes, the white board is erased completely.
sarrow.gifSee "Working with Variables," p. 159
Variable Declarations
The first part of the Calculate sub procedure will be used to declare the variables you'll need. That means that you will tell Visual Basic the names of the variables that the procedure will be using, as well as what type of information each variable may contain. Although Visual Basic doesn't require you to declare your variables (by default), it's always good practice to do so.
tip.gif
Always declare your variables! In the Tools, Options menu, under the Editor tab, make sure Require Variable Declaration is checked. This causes Visual Basic to report an error if you attempt to run a program that uses a variable that hasn't been declared (for example, if you inadvertently misspell the variable's name).

Your Calculate procedure uses four variablesone each to hold the principal amount, the interest rate, the loan term, and the calculated monthly payment. Make sure the cursor is on the blank line between Private Sub cmdCalculate_Click() and End Sub. Press Tab to indent the code, and then type the following line of code:
    Dim cPrincipal As Currency
Notice that when you press Enter after typing this code, the cursor doesn't go back to the left margin of the code window. Visual Basic assumes that you want to indent the next line of code to the same level as the preceding line. This aids in your code's readability. You can increase the level of indent by pressing Tab; Backspace will decrease the indent.
As you can tell from this code, the general format for declaring variables is the word Dim, followed by a variable name, the word As, and the variable's type. As with object names, you should follow a naming standard. Throughout this book, you'll use a very common variable naming standard that uses the first (lowercase) character of the variable name as a prefix designating its type; the remaining portion of the variable's name describes its purpose. The variable named cPrincipal, for example, is a Currency type variable that stores the principal amount. Some variable types and their common prefixes are outlined in Table 2.3.

 
< previous page page_34 next page >